import threading
import time
# ------------死锁----------------
class Mythread(threading.Thread):
    '''递归锁 解决死锁'''
    def actionA(self):
        A.acquire()  # count=1
        print(self.name, "got A", time.ctime())
        B.acquire()  # 锁中锁 count=2
        print(self.name, "got B", time.ctime())
        B.release()  # count = 1
        A.release()  # count = 0
    def actionB(self):
        B.acquire()
        print(self.name, "got B", time.ctime())
        A.acquire()# 锁中锁
        print(self.name, "got A", time.ctime())
        A.release()
        B.release()
    def run(self):
        self.actionA()
        self.actionB()
if __name__ == "__main__":
    L = []
    A=threading.Lock()
    B=threading.Lock()
    for i in range(5):
        t = Mythread()
        t.start()
        L.append(t)
    for t in L:
        t.join()
    print("ending>>>>>>>>>>")

# -------------递归锁--------------
# class Mythread(threading.Thread):
#     '''递归锁 解决死锁'''
#     def actionA(self):
#         r_lock.acquire()  # count=1
#         print(self.name, "got A", time.ctime())
#         r_lock.acquire()  # 锁中锁 count=2
#         print(self.name, "got B", time.ctime())
#         r_lock.release()  # count = 1
#         r_lock.release()  # count = 0
#     def actionB(self):
#         r_lock.acquire()
#         print(self.name, "got B", time.ctime())
#         r_lock.acquire()# 锁中锁
#         print(self.name, "got A", time.ctime())
#         r_lock.release()
#         r_lock.release()
#     def run(self):
#         self.actionA()
#         self.actionB()
# if __name__ == "__main__":
#     L = []
#     r_lock=threading.RLock()
#     for i in range(5):
#         t = Mythread()
#         t.start()
#         L.append(t)
#     for t in L:
#         t.join()
#     print("ending>>>>>>>>>>")